//
//  YLHomeProductViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/18.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLHomeProductViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "YLProductItem.h"
#import "WHProgressHUD.h"

@interface YLHomeProductViewModel ()

/**
 分组的标题
 */
@property (nonatomic, strong) NSArray *icons;


/**
 分组的标签
 */
@property (nonatomic, strong) NSArray *tags;


@end


@implementation YLHomeProductViewModel

- (instancetype)init
{
    if (self = [super init]) {
        _groups = [NSArray array];
        _icons = @[@"new", @"RecommendedToday", @"ExclusiveMembership"];
        _tags = @[@"  仅限首投  ", @"  热销  ", @"  会员分级购买  "];
        [self setUp];
    }
    return self;
}


- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    _productCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                        
            NSString *url = [NSString stringWithFormat:@"%@Product/PGetHomeProducts", YLURL];
            
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                
                [WHProgressHUD dismiss];
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                    YLProductItem *productItem = [YLProductItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                    
                    for(YLHomeProductGroup *group in productItem.item) {
                        
                        NSInteger type = group.platformType;
                        
                        if (type == 0) {
                            break;
                        }
                        type = type - 1;
                        
                        if (type < weakSelf.icons.count) {
                            group.tag = weakSelf.tags[type];
                            group.icon = weakSelf.icons[type];
                        } else {
                            group.tag = @"";
                            group.icon = weakSelf.icons.firstObject;
                        }
                    }
                    weakSelf.groups = productItem.item;
                    
                    [subscriber sendNext:nil];
                    
                } else {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
                
                [subscriber sendCompleted];
            } fail:^(NSError *error) {
                [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
    }];
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}


@end
